using System;
using System.ComponentModel;
using System.DirectoryServices;
using RollManager.Library.ActionSteps.BaseClasses;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class IISSiteSetStep : IISStep
    {
        public string m_propertyValue = StepConstants.NAME;

        [DisplayName( "Property Value" )]
        [Description( "The value to which the property will be set." )]
        public string PropertyValue
        {
            get
            {
                return m_propertyValue;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Property Value cannot be empty." );
                }
                else
                {
                    m_propertyValue = value.Trim();
                }
            }
        }

        public override string Name
        {
            get { return "IIS Set Site Property"; }
        }

        protected override void Run()
        {
            DirectoryEntry entry = getEntry();

            // Set property
            entry.InvokeSet( PropertyName, m_propertyValue );
            entry.CommitChanges();

            if( string.IsNullOrEmpty( WebSite ) )
            {
                Progress.AddFormat( "Set {0} to {1}", PropertyPath, m_propertyValue );
            }
            else
            {
                Progress.AddFormat( "Set {0} to {1} on {2}", PropertyPath, m_propertyValue, WebSite );
            }
        }

        protected override void Simulate()
        {
            DirectoryEntry entry = getEntry();
            object property = entry.InvokeGet( PropertyName );

            if( string.IsNullOrEmpty( WebSite ) )
            {
                Progress.AddFormat( "{0}, which is currently, {1} would change to {2}",
                    PropertyPath, displayProperty( property ), m_propertyValue );
            }
            else
            {
                Progress.AddFormat( "{0}, which is currently, {1} would change to {2} on {3}",
                    PropertyPath, displayProperty( property ), m_propertyValue, WebSite );
            }
        }
    }
}
